import 'package:get/get.dart';
import 'package:xmshop/app/models/category_model.dart';
import 'package:xmshop/app/services/httpsClient.dart';

class CategoryController extends GetxController {
  //TODO: Implement CategoryController

  RxInt selectIndex = 0.obs;
  //顶级分类数据
  RxList<CategoryItemModel> leftCategoryList = <CategoryItemModel>[].obs;
  RxList<CategoryItemModel> rightCategoryList = <CategoryItemModel>[].obs;
  HttpsClient httpsClient = HttpsClient();
  @override
  void onInit() {
    super.onInit();
    getCategoryData();
  }

  getCategoryData() async {
    // var response = await Dio().get("https://miapp.itying.com/api/pcate");
    var response = await httpsClient.get("api/pcate");
    if (response != null) {
      print(response.data["result"]);
      var focus = CategoryModel.fromJson(response.data);
      leftCategoryList.value = focus.result!;
      getRightCategoryData(leftCategoryList[0].sId!);
      update();
    }
  }

  getRightCategoryData(String pid) async {
    // var response = await Dio().get(
    //   "https://miapp.itying.com/api/pcate?pid=$pid",
    // );
    var response = await httpsClient.get("api/pcate?pid=$pid");
    if (response != null) {
      print(response.data["result"]);
      var focus = CategoryModel.fromJson(response.data);
      rightCategoryList.value = focus.result!;
      update();
    }
  }

  void changeIndex(index, id) {
    selectIndex.value = index;
    getRightCategoryData(id);
    update();
  }
}
